/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package it.trick.admin.cell;

import it.trick.admin.model.ChapterModel;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.control.ContextMenu;
import javafx.scene.control.MenuItem;
import javafx.scene.control.SeparatorMenuItem;
import javafx.scene.control.TreeCell;
import javafx.scene.control.TreeItem;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author takahara
 */
public class ChapterTreeViewCell extends TreeCell<ChapterModel> {

    TreeCell treeCell;
    private ContextMenu contextMenu = new ContextMenu();

    /**
     * コンストラクタ
     */
    public ChapterTreeViewCell() {
        MenuItem addChapterMenuItem = new MenuItem("ページ追加");
        MenuItem changeIndexUpperMenuItem = new MenuItem("上へ ↑");
        MenuItem changeIndexLowerMenuItem = new MenuItem("下へ ↓");
        MenuItem deleteCategoryMenuItem = new MenuItem("削除");
        contextMenu.getItems().addAll(
                addChapterMenuItem,
                new SeparatorMenuItem(),
                changeIndexUpperMenuItem,
                changeIndexLowerMenuItem,
                new SeparatorMenuItem(),
                deleteCategoryMenuItem);


        // 追加イベント
        addChapterMenuItem.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
            }
        });

        // 上移動イベント
        changeIndexUpperMenuItem.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                ObservableList<TreeItem<ChapterModel>> nodeList = treeCell.getTreeView().getRoot().getChildren();
                int oldIndex = getIndex();
                int newIndex = oldIndex - 1;
                if(oldIndex > 0) {
                    TreeItem targetItem = nodeList.get(newIndex);
                    TreeItem soceItem = nodeList.get(oldIndex);
                    nodeList.set(newIndex, soceItem);
                    nodeList.set(oldIndex, targetItem);

                    treeCell.getTreeView().getSelectionModel().select(newIndex);
                }
            }
        });

        // 下移動イベント
        changeIndexLowerMenuItem.addEventHandler(ActionEvent.ACTION, new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent t) {
                ObservableList<TreeItem<ChapterModel>> nodeList = treeCell.getTreeView().getRoot().getChildren();
                int listSize = nodeList.size();
                int oldIndex = getIndex();
                int newIndex = oldIndex + 1;
                if(oldIndex + 1 < listSize) {
                    TreeItem targetItem = nodeList.get(newIndex);
                    TreeItem soceItem = nodeList.get(oldIndex);
                    nodeList.set(newIndex, soceItem);
                    nodeList.set(oldIndex, targetItem);

                    treeCell.getTreeView().getSelectionModel().select(newIndex);
                }
            }
        });

        setTreeCell();
    }

    private void setTreeCell() {
        treeCell = this;
    }

    /**
     * セル更新
     *
     * @param item
     * @param empty
     */
    @Override
    public void updateItem(ChapterModel item, boolean empty) {
        super.updateItem(item, empty);

        if (empty) {
            setText(null);
            setGraphic(null);
        } else {
            super.setText(item.getName());
            setGraphic(getTreeItem().getGraphic());

            setContextMenu(contextMenu);

            // コンテキストメニュー表示イベント
            this.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent t) {
                    if (t.isControlDown()) {
                        getContextMenu().show(treeCell, t.getScreenX(), t.getScreenY());
                    }
                }
            });
        }
    }
}
